using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using LuaInterface;

namespace LuaFramework {
    public class PanelManager : Manager {
        //private Transform parent;

        //Transform Parent {
        //    get {
        //        if (parent == null) {
        //            GameObject go = GameObject.FindWithTag("GuiCamera");
        //            if (go != null) parent = go.transform;
        //        }
        //        return parent;
        //    }
        //}

        /// <summary>
        /// ������壬������Դ������
        /// </summary>
        /// <param name="type"></param>
        public void CreatePanel(string name, LuaFunction func = null) {
//            string assetName = name + "Panel";
//            string abName = name.ToLower() + AppConst.ExtName;
//            if (Parent.Find(name) != null) return;

//#if ASYNC_MODE
//            ResManager.LoadPrefab(abName, assetName, delegate(UnityEngine.Object[] objs) {
//                if (objs.Length == 0) return;
//                GameObject prefab = objs[0] as GameObject;
//                if (prefab == null) return;

//                GameObject go = Instantiate(prefab) as GameObject;
//                go.name = assetName;
//                go.layer = LayerMask.NameToLayer("Default");
//                go.transform.SetParent(Parent);
//                go.transform.localScale = Vector3.one;
//                go.transform.localPosition = Vector3.zero;
//                go.AddComponent<LuaBehaviour>();

//                if (func != null) func.Call(go);
//                Debug.LogWarning("CreatePanel::>> " + name + " " + prefab);
//            });
//#else
//            GameObject prefab = ResManager.LoadAsset<GameObject>(name, assetName);
//            if (prefab == null) return;

//            GameObject go = Instantiate(prefab) as GameObject;
//            go.name = assetName;
//            go.layer = LayerMask.NameToLayer("Default");
//            go.transform.SetParent(Parent);
//            go.transform.localScale = Vector3.one;
//            go.transform.localPosition = Vector3.zero;
//            go.AddComponent<LuaBehaviour>();

//            if (func != null) func.Call(go);
//            Debug.LogWarning("CreatePanel::>> " + name + " " + prefab);
//#endif
        }

        /// <summary>
        /// �ر����
        /// </summary>
        /// <param name="name"></param>
        public void ClosePanel(string name) {
            //var panelName = name + "Panel";
            //var panelObj = Parent.Find(panelName);
            //if (panelObj == null) return;
            //Destroy(panelObj.gameObject);
        }

        private Transform mainParent;
        private Transform uiParent;
        private Transform topParent;

        public Transform MainParent
        {
            get
            {
                if (mainParent == null)
                {
                    GameObject go = GameObject.FindWithTag("Main");
                    if (go != null)
                    {
                        mainParent = go.transform;
                    }
                }
                return mainParent;
            }
        }

        public Transform UIParent
        {
            get
            {
                if (uiParent == null)
                {
                    GameObject go = GameObject.FindWithTag("UI");
                    if (go != null)
                    {
                        uiParent = go.transform;
                    }
                }
                return uiParent;
            }
        }

        public Transform TopParent
        {
            get
            {
                if (topParent == null)
                {
                    GameObject go = GameObject.FindWithTag("Top");
                    if (go != null)
                    {
                        topParent = go.transform;
                    }
                }
                return topParent;
            }
        }

        public Transform GetParent(string name)
        {
            switch (name)
            {
                case "Main":
                    return MainParent;
                case "UI":
                    return UIParent;
                case "Top":
                    return topParent;
                default: return null;
            }
            return null;
        }

        /// <summary>
        /// 创建界面
        /// </summary>
        /// <param name="abName"></param>
        /// <param name="pfName"></param>
        /// <param name="layer"></param>
        /// <param name="func"></param>
        public void CreateView(string abName, string pfName, string parentName = "", LuaFunction func = null)
        {
            string assetBundleName = abName.ToLower() + AppConst.ExtName;
            //if (Parent.Find(pfName) != null) return;
            Transform parent = UIParent;
            if (parentName != "")
            {
                parent = GetParent(parentName);
            }

#if ASYNC_MODE
            ResManager.LoadPrefab(assetBundleName, pfName, delegate (UnityEngine.Object[] objs)
            {
                if (objs == null || objs.Length == 0) return;
                GameObject prefab = objs[0] as GameObject;
                if (prefab == null) return;

                GameObject go = Instantiate(prefab) as GameObject;
                go.name = prefab.name;
                go.layer = LayerMask.NameToLayer("UI");
                go.transform.SetParent(parent);
                go.transform.localScale = Vector3.one;
                go.transform.localPosition = Vector3.zero;

                if (func != null) func.Call(go);
                Debug.LogWarning("CreateView::>> " + assetBundleName + "  " + pfName);
            });
#else

#endif
        }

        /// <summary>
        /// 创建item
        /// </summary>
        /// <param name="abName"></param>
        /// <param name="pfName"></param>
        /// <param name="func"></param>
        public void CreateItem(string abName, string pfName, Transform _parent = null, LuaFunction func = null)
        {
            string assetBundleName = abName.ToLower() + AppConst.ExtName;
            //if (Parent.Find(pfName) != null) return;

#if ASYNC_MODE
            ResManager.LoadPrefab(assetBundleName, pfName, delegate (UnityEngine.Object[] objs)
            {
                if (objs == null || objs.Length == 0) return;
                GameObject prefab = objs[0] as GameObject;
                if (prefab == null) return;

                GameObject go = Instantiate(prefab) as GameObject;
                go.name = prefab.name;
                if (_parent != null)
                {
                    go.transform.SetParent(_parent);
                }
                go.transform.localScale = Vector3.one;
                go.transform.localPosition= Vector3.zero;

                if (func != null) func.Call(go);
                Debug.LogWarning("CreateItem::>> " + assetBundleName + "  " + pfName);
            });
#else

#endif
        }
    }
}